/**
 * 
 */
package com.jxc.action.xsgl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VUser;
import com.jxc.service.UserServiceI;
import com.jxc.util.ExceptionUtil;
import com.jxc.util.ResourceUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @author Tino
 * 
 */
@Namespace("/xsgl")
@Action(value = "pointTacticsAction", results = { @Result(name = "pointTactics", location = "/sale/pointTactics.jsp"),
		                                      @Result(name = "pointTacticsEdit", location = "/sale/pointTacticsEdit.jsp")})
public class PointTacticsAction extends BaseAction implements ModelDriven<VUser> {
	private final static Logger logger = Logger.getLogger(PointTacticsAction.class);

	@Autowired
	private UserServiceI userService;
	
	private VUser model = new VUser();
	
	@Override
	public VUser getModel() {
		return model;
	}
	
	public String pointTactics() {
		getSubAuths("XSGL_JFCL");
		return "pointTactics";
	}	
	
	public String pointTacticsEdit() {
		getSubAuths("XSGL_JFCL_EDIT");
		return "pointTacticsEdit";
	}	
	
	public void datagridPointTactics() {
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		    List<VUser> vuser = new ArrayList<VUser>();
			VUser vv =  userService.get(sessionInfo.getUserId());
			vuser.add(vv);
			this.writeJson(vuser);
		} catch (Exception e) {
			e.printStackTrace();
		}
			
	}
	
	public void noAuthDatagridPointTactics() {
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		    List<VUser> vuser = new ArrayList<VUser>();
		    VUser vv = new VUser();
		    if(userService.get(sessionInfo.getUserId()).getUpid()!=null){
				vv = userService.get(userService.get(sessionInfo.getUserId()).getUpid()); 
		    }else if(userService.get(sessionInfo.getUserId()).getUserType()==1||userService.get(sessionInfo.getUserId()).getUserType()==1){
				vv = userService.get(sessionInfo.getUserId()); 
		    }
			vuser.add(vv);
			super.writeJson(vuser);
		} catch (Exception e) {
			e.printStackTrace();
		}
			
	}
	
	public void edit() {
		Json j = new Json();
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
			model.setId(sessionInfo.getUserId());
			userService.changePointTactics(model);
			j.setMsg("修改成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg("修改失败！");
			logger.info(ExceptionUtil.getExceptionMessage(e));
		}
		super.writeJson(j);

	}
}
